﻿using blackhouse.JavaScript.JQuery;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.WebNemo.Web.Manager {
    public partial class Default : System.Web.UI.Page {

        #region Fields

        private Factory factory = Factory.Instance();
        private IGroup[] groups;

        #endregion

        #region Event Handlers

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.SetScenario();
            this.PopulateView();
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeJQueryUI();
        }

        #endregion

        #region Methods

        private void SetScenario() {
            this.groups = this.factory.GetGroups().OrderBy(g => g.Name).ToArray();
        }

        private void PopulateView() {
            NemoJsLibrary njl = new NemoJsLibrary();
            njl.IncludePopup();
            njl.IncludeScroller();

            this.rptGroups.DataSource = this.groups;
            this.rptGroups.DataBind();

            NemoUrl url = new NemoUrl(NemoUrlView.EditGroup);
            url.GroupType = GroupType.Table;
            this.lnkAddNewGroup.NavigateUrl = url.GetUrl();
            url.GroupType = GroupType.View;
            this.lnkAddNewView.NavigateUrl = url.GetUrl();
        }

        protected void rptGroups_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    IGroup group = e.Item.DataItem as IGroup;
                    if (group != null) {
                        Panel pnlWrapper = e.Item.FindControl("pnlWrapper") as Panel;
                        Literal ltlGroupType = e.Item.FindControl("ltlGroupType") as Literal;
                        Panel pnlReadOnly = e.Item.FindControl("pnlReadOnly") as Panel;
                        HyperLink lnkEditGroup = e.Item.FindControl("lnkEditGroup") as HyperLink;
                        HyperLink lnkRecords = e.Item.FindControl("lnkRecords") as HyperLink;
                        HyperLink lnkColumns = e.Item.FindControl("lnkColumns") as HyperLink;

                        if (pnlWrapper != null) {
                            string css = "group-item-" + group.Type.ToString().ToLower();
                            if (group.ReadOnly)
                                css = "read-only-" + css;
                            if (!String.IsNullOrEmpty(pnlWrapper.CssClass))
                                css = pnlWrapper.CssClass + ' ' + css;
                            pnlWrapper.CssClass = css;
                        }
                        if (ltlGroupType != null) {
                            switch (group.Type) {
                                case GroupType.Table:
                                    ltlGroupType.Text = "Tabela";
                                    break;
                                case GroupType.View:
                                    ltlGroupType.Text = "Widok";
                                    break;
                                case GroupType.ExtendedTable:
                                    ltlGroupType.Text = "Rozszerzenie tabeli";
                                    break;
                                default:
                                    ltlGroupType.Text = "b.d.";
                                    break;
                            }
                        }
                        if (pnlReadOnly != null && group.ReadOnly)
                            pnlReadOnly.Visible = true;
                        if (lnkEditGroup != null) {
                            NemoUrl url = new NemoUrl(NemoUrlView.EditGroup);
                            url.GroupType = group.Type;
                            url.GroupId = group.Id;
                            url.AddReturnUrl = true;
                            lnkEditGroup.NavigateUrl = url.GetUrl();
                        }
                        if (lnkRecords != null) {
                            lnkRecords.Attributes.Add("data-type", "open-popup");
                            NemoUrl url = new NemoUrl(NemoUrlView.RecordList);
                            url.GroupId = group.Id;
                            url.AutoClose = true;
                            lnkRecords.NavigateUrl = url.GetUrl();
                        }
                        if (lnkColumns != null) {
                            NemoUrl url = new NemoUrl(NemoUrlView.ManageColumns);
                            url.GroupId = group.Id;
                            lnkColumns.NavigateUrl = url.GetUrl();
                        }
                    }
                    else
                        e.Item.Visible = false;
                    break;
            }
        }

        protected void rptGroups_ItemCommand(object source, RepeaterCommandEventArgs e) {
            switch (e.CommandName) {
                case "remove":
                    int groupId = 0;
                    if (Int32.TryParse(e.CommandArgument.ToString(), out groupId)) {
                        IGroup group = new Group(groupId);
                        group.Delete();
                        Response.Redirect("/Nemo/Manager/Default.aspx?message=recordRemoved");
                    }
                    break;
                default:
                    break;
            }
        }

        #endregion

    }
}